﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dapper;
using System.Data.OracleClient;
using System.Data;

namespace _180FrameWork.DataBaseHelper.OracleExtend
{ 
    #region
    /*
    public class OracleDynamicParameters : SqlMapper.IDynamicParameters
    {
       
        private readonly DynamicParameters _dynamicParameters = new DynamicParameters();

        private readonly List<OracleParameter> _oracleParameters = new List<OracleParameter>();

        public void Add(string name, object value = null, DbType dbType = DbType.AnsiString, ParameterDirection? direction = null, int? size = null)
        {
            _dynamicParameters.Add(name, value, dbType, direction, size);
        }

        public void Add(string name, OracleType oracleDbType, ParameterDirection direction)
        {
            var oracleParameter = new OracleParameter(name, oracleDbType) { Direction = direction };
            _oracleParameters.Add(oracleParameter);
        }

        public void Add(string name, OracleType oracleDbType, int size, ParameterDirection direction)
        {
            var oracleParameter = new OracleParameter(name, oracleDbType, size) { Direction = direction };
            _oracleParameters.Add(oracleParameter);
        }

        public void AddParameters(IDbCommand command, SqlMapper.Identity identity)
        {
            ((SqlMapper.IDynamicParameters)_dynamicParameters).AddParameters(command, identity);

            var oracleCommand = command as OracleCommand;

            if (oracleCommand != null)
            {
                oracleCommand.Parameters.AddRange(_oracleParameters.ToArray());
            }
        }

        public T Get<T>(string parameterName)
        {
            var parameter = _oracleParameters.SingleOrDefault(t => t.ParameterName == parameterName);
            if (parameter != null)
                return (T)Convert.ChangeType(parameter.Value, typeof(T));
            return default(T);
        }

        public T Get<T>(int index)
        {
            var parameter = _oracleParameters[index];
            if (parameter != null)
                return (T)Convert.ChangeType(parameter.Value, typeof(T));
            return default(T);
        }
      
    }  */
    #endregion

    public class OracleDynamicParameters : SqlMapper.IDynamicParameters
    {
        private readonly List<OracleParameter> _oracleParameters = new List<OracleParameter>();
        public List<OracleParameter> OracleParameters { get { return this._oracleParameters; } }
        public void Add(string name, object value, OracleType oracleDbType,int size=0, ParameterDirection direction = ParameterDirection.Input)
        {
            var oracleParameter = new OracleParameter(name, value);          
            oracleParameter.OracleType = oracleDbType;
            if (size > 0)
                oracleParameter.Size = size;
            oracleParameter.Direction = direction;
            _oracleParameters.Add(oracleParameter);
        }
        public void AddParameters(IDbCommand command, SqlMapper.Identity identity)
        {
            var oracleCommand = command as OracleCommand;

            if (oracleCommand != null)
            {
                oracleCommand.Parameters.AddRange(_oracleParameters.ToArray());
            }
        }
        public T Get<T>(string parameterName)
        {
            var parameter = _oracleParameters.SingleOrDefault(t => t.ParameterName == parameterName);
            if (parameter != null)
                return (T)Convert.ChangeType(parameter.Value, typeof(T));
            return default(T);
        }

        public T Get<T>(int index)
        {
            var parameter = _oracleParameters[index];
            if (parameter != null)
                return (T)Convert.ChangeType(parameter.Value, typeof(T));
            return default(T);
        }
       
        //public object Get(string parameterName)
        //{
        //    var parameter = _oracleParameters.SingleOrDefault(t => t.ParameterName == parameterName);
        //    if (parameter != null)
        //        return parameter.Value;
        //    return null;
        //}

        //public object Get(int index)
        //{
        //    var parameter = _oracleParameters[index];
        //    if (parameter != null)
        //        return parameter.Value;
        //    return null;
        //}
    }

}
